package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.AttendanceData;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Megan on 16/4/11.
 */
public class GetAttendanceList extends CcMsgStructure {

    private static final String TAG = "GetAttendanceList";

    private Message mmCallback;
    private Context mContext;
    private CloudDBOperation cloudDBOperation;

    public GetAttendanceList(Message callback, Context context) {
        mmCallback = callback;
        mContext = context;
        cloudDBOperation = new CloudDBOperation(mContext);
    }

    @Override
    public void onReceive(JSONObject data) {
        ArrayList<AttendanceData> attendanceDatas = new ArrayList<AttendanceData>();
        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            JSONArray jsonArray = jsonObject.getJSONArray(AsynHttpClient.KEY_ATTENDANCES);
            for (int i = 0; i < jsonArray.length(); i++) {
                AttendanceData attendanceData = new AttendanceData();
                JSONObject item = jsonArray.getJSONObject(i);
                attendanceData.set_id(item.getString("_id"));
                attendanceData.setDeviation(item.getString("deviation"));
                attendanceData.setName(item.getString("name"));
                attendanceData.setMemberCount(item.getString("member_count"));
                attendanceData.setLongitude(item.getString("longitude"));
                attendanceData.setLatitude(item.getString("latitude"));
                attendanceData.setAddress(item.getString("address"));
                attendanceData.setWorkDate(item.getString("work_date").replace("[","").replace("]",""));
                if (item.has("form_type")){
                    int formType = item.getInt("form_type");
                    attendanceData.setFormType(formType);
                    if (formType == 0){
//                        attendanceData.setIsQuartic(item.getInt("is_quartic"));
                        attendanceData.setEndWorkTime(item.getString("end_work_time"));
                        attendanceData.setStartWorkTime(item.getString("start_work_time"));
                        if (item.has("noon_start_time")) {
                            attendanceData.setIsQuartic(1);
                            attendanceData.setNoonStartTime(item.getString("noon_start_time"));
                        }
                        if (item.has("noon_end_time")) {
                            attendanceData.setNoonEndTime(item.getString("noon_end_time"));
                        }

                    }else if(formType == 1){
                        if (item.has("sub_forms")){
                            attendanceData.setSubForms(item.getJSONArray("sub_forms").toString());
                        }
                    }
                }else{
                    attendanceData.setIsQuartic(0);
                    attendanceData.setEndWorkTime(item.getString("end_work_time"));
                    attendanceData.setStartWorkTime(item.getString("start_work_time"));
                    if (item.has("noon_start_time")) {
                        attendanceData.setNoonStartTime(item.getString("noon_start_time"));
                        attendanceData.setIsQuartic(1);
                    }
                    if (item.has("noon_end_time")) {
                        attendanceData.setNoonEndTime(item.getString("noon_end_time"));
                    }
                }

                if (item.has("cached")) {
                    JSONObject cached = item.getJSONObject("cached");
                    if (cached.has("start_work_time")) {
                        attendanceData.setStartWorkTime(cached.getString("start_work_time"));
                    }
                    if (cached.has("end_work_time")) {
                        attendanceData.setEndWorkTime(cached.getString("end_work_time"));
                    }
                    if (cached.has("noon_start_time")) {
                        attendanceData.setNoonStartTime(cached.getString("noon_start_time"));
                    }
                    if (cached.has("noon_end_time")) {
                        attendanceData.setNoonEndTime(cached.getString("noon_end_time"));
                    }
                    if (cached.has("work_date")) {
                        String cacheDate = cached.getString("work_date");
                        cacheDate = cacheDate.replace("[","").replace("]","");
                        attendanceData.setWorkDate(cacheDate);
                    }
                    if (cached.has("is_quartic")) {
                        attendanceData.setIsQuartic(cached.getInt("is_quartic"));
                    }
                }
                if (item.has("wifi")) {
                    attendanceData.setWifiMsg(item.getString("wifi"));
                }

                attendanceDatas.add(attendanceData);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (mmCallback != null)

        {
            mmCallback.obj = attendanceDatas;
            mmCallback.arg1 = 0;
            mmCallback.sendToTarget();
            return;
        }

    }

    @Override
    public void onError(int error, Object ret) {
        Log.d(TAG, "onError : " + error + " ret: " + ret);
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if (mmCallback != null) {
            mmCallback.arg1 = error;
            if (!StringUtil.isEmpty(errorMsg)) {
                mmCallback.obj = errorMsg;
            } else {
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }

    }
}